// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/service/module/images_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/images"
	imagesModel "gin-sns/app/model/module/images"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var ImagesService = newImagesService()

func newImagesService() *imagesService {
	return &imagesService{}
}

type imagesService struct {
}

//根据主键查询数据
func (s *imagesService) SelectRecordById(id int64) (*imagesModel.Entity, error) {
	return images.ImagesDao.SelectById(id)
}

//
func (s *imagesService) SelectRecordsIn(idarr []int64) ([]imagesModel.Entity, error) {

	list, err := images.ImagesDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *imagesService) DeleteRecordById(id int64) bool {
	return images.ImagesDao.DeleteById(id)
}

//批量删除数据记录
func (s *imagesService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := images.ImagesDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *imagesService) AddSave(req *imagesModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity imagesModel.Entity

	//entity.Id = req.Id
	entity.BlogId = req.BlogId
	entity.UserId = req.UserId
	entity.ImgUrl = req.ImgUrl
	entity.ImgName = req.ImgName
	entity.Description = req.Description
	entity.FileSize = req.FileSize
	entity.ImgWidth = req.ImgWidth
	entity.ImgHeight = req.ImgHeight
	entity.Latitude = req.Latitude
	entity.Longitude = req.Longitude
	entity.Signature = req.Signature
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := images.ImagesDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *imagesService) EditSave(req *imagesModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &imagesModel.Entity{Id: req.Id}
	ok, err := images.ImagesDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.BlogId = req.BlogId
	entity.UserId = req.UserId
	entity.ImgUrl = req.ImgUrl
	entity.ImgName = req.ImgName
	entity.Description = req.Description
	entity.FileSize = req.FileSize
	entity.ImgWidth = req.ImgWidth
	entity.ImgHeight = req.ImgHeight
	entity.Latitude = req.Latitude
	entity.Longitude = req.Longitude
	entity.Signature = req.Signature
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return images.ImagesDao.Update(entity)
}

//根据条件查询数据
func (s *imagesService) SelectListAll(params *imagesModel.SelectPageReq) ([]imagesModel.Entity, error) {
	return images.ImagesDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *imagesService) SelectListByPage(params *imagesModel.SelectPageReq) ([]imagesModel.Entity, *page.Paging, error) {
	return images.ImagesDao.SelectListByPage(params)
}

// 导出excel
func (s *imagesService) Export(param *imagesModel.SelectPageReq) (string, error) {
	head := []string{"", "博客id", "用户id", "图片URL", "图片名称", "图片描述", "图片文件大小", "图片宽度", "图片高度", "图片位置LAT", "图片位置LONG", "图片签名", "0未审核 1正常状态 2审核未通过 3删除", "创建时间", "更新时间"}
	col := []string{"id", "blog_id", "user_id", "img_url", "img_name", "description", "file_size", "img_width", "img_height", "latitude", "longitude", "signature", "status", "create_time", "update_time"}
	return images.ImagesDao.SelectListExport(param, head, col)
}
